package com.sm.controller;

import com.sm.entity.*;
import com.sm.service.FinancialService;
import com.sm.utils.AttendanceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/financial")
public class FinancialController {

    @Autowired
    FinancialService financialService;

    @RequestMapping("/financial")
    public String  financial(){
        return "admin/financial/financial";
    }

    @ResponseBody
    @RequestMapping("/days")
    public ResultMap dayFinancial()throws Exception{
        List<FinancialRec> today =  financialService.findFinancialRec(AttendanceUtil.getDateStr());
        List<FinancialRec> yesterday =  financialService.findFinancialRec(AttendanceUtil.getYseterdayStr());
        ResultMap resultMap = new ResultMap();
        resultMap.setResult("today",today);
        resultMap.setResult("yesterday",yesterday);
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/recently")
    public ResultMap recFinancial()throws Exception{
        List<FinancialRec> income =  financialService.findSevenDaysFRIncome();
        List<FinancialRec> profit =  financialService.findSevenDaysFRProfit();
        ResultMap resultMap = new ResultMap();
        resultMap.setResult("income",income);
        resultMap.setResult("profit",profit);
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/ftable")
    public ResultMap tableFinancial()throws Exception{
       List<Financial> ftable = financialService.findFinancial(AttendanceUtil.getMonth());
        ResultMap resultMap = new ResultMap();
       resultMap.setResult("ftable",ftable);
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/add")
    public ResultMap addFinancial(Financial financial)throws Exception{
        UserLogin userLogin = (UserLogin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(userLogin.getUid());
        financial.setUpdatetime(new Date());
        financial.setMonth(AttendanceUtil.getMonth());
        financial.setUserInfo(userInfo);
        System.out.println("\n\n\n\n\n");
        System.out.println(financial);
        System.out.println("\n\n\n\n\n");
        financialService.addFinancial(financial);
        ResultMap resultMap = new ResultMap();
        resultMap.setMessage("添加成功");
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/statistics")
    public ResultMap financialStatistics()throws Exception{
        List<FinancialRec> list = financialService.financialStatistics();
        ResultMap resultMap = new ResultMap();
        resultMap.setResult("statistics",list);
        return resultMap;
    }
}
